package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.mapper.ApBehaviorEntryMapper;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import org.springframework.stereotype.Service;

/**
 * @作者 itcast
 * @创建日期 2021/2/4 9:50
 **/
@Service
public class ApBehaviorEntryServiceImpl extends ServiceImpl<ApBehaviorEntryMapper, ApBehaviorEntry> implements ApBehaviorEntryService {
    @Override
    public ApBehaviorEntry findByUserIdOrEquipmentId(Integer userId, Integer equipmentId) {
        // 1. 判断userId是否为空，不为空按照userId查询实体   type=1
        if(userId!=null){
            return getOne(Wrappers.<ApBehaviorEntry>lambdaQuery()
                    .eq(ApBehaviorEntry::getEntryId, userId)
                    .eq(ApBehaviorEntry::getType, ApBehaviorEntry.Type.USER.getCode())
            );
        }
        // 2. userId为空 设备ID不为空 不为0  按照设备ID查询   type=0
        if(equipmentId!=null && equipmentId!=0){
            return getOne(Wrappers.<ApBehaviorEntry>lambdaQuery()
                    .eq(ApBehaviorEntry::getEntryId, equipmentId)
                    .eq(ApBehaviorEntry::getType, ApBehaviorEntry.Type.EQUIPMENT.getCode())
            );
        }
        // 3. 都为空返回null
        return null;
    }

}
